package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CateGoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CateGoryService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CateGoryMapper cateGoryMapper;
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO,setmeal);
        int count = setmealMapper.selectCount(setmeal);
        int start,end;
        //计算开始下标
        start = (setmealPageQueryDTO.getPage()-1) * setmealPageQueryDTO.getPageSize() < 0 ? 0 :
                (setmealPageQueryDTO.getPage()-1) * setmealPageQueryDTO.getPageSize();
        System.out.println((setmealPageQueryDTO.getPage()-1) * setmealPageQueryDTO.getPageSize());
        //计算需要几条数据
        end = setmealPageQueryDTO.getPage() * setmealPageQueryDTO.getPageSize() <= count ?
                setmealPageQueryDTO.getPageSize() :
                (count - (setmealPageQueryDTO.getPage()-1) * setmealPageQueryDTO.getPageSize() < 0 ? count :
                        count - (setmealPageQueryDTO.getPage()-1) * setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmeals = setmealMapper.select(setmeal,start,end);
        List<SetmealDTO> records = new ArrayList<>();
        for (Setmeal s: setmeals) {
            SetmealDTO setmealDTO = new SetmealDTO();
            BeanUtils.copyProperties(s,setmealDTO);
            setmealDTO.setCategoryName(cateGoryMapper.selectByid(s.getCategoryId()));
            records.add(setmealDTO);
        }
        return new PageResult(count,records);
    }
//新增套餐
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(1);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.addSetmeal(setmeal);
        Long id = setmealMapper.selectByname(setmeal.getName());
        if (setmealDTO.getSetmealDishes()!=null){
            for (SetmealDish setmealDish: setmealDTO.getSetmealDishes()) {
                setmealDish.setSetmealId(id);
                setmealDishMapper.addDish(setmealDish);
            }
        }
    }
//删除套餐
    @Override
    public void deleteSmeal(String ids) {
        String[] idss = ids.split(",");
        for (String s: idss) {
            setmealMapper.deleteSmeal(Long.valueOf(s));
            setmealDishMapper.deleteDish(Long.valueOf(s));
        }

    }
//修改套餐
    @Override
    public void updateSmeal(SetmealDTO setmealDTO) {
        System.out.println(setmealMapper.selectByname(setmealDTO.getName()));
        deleteSmeal(String.valueOf(setmealMapper.selectByname(setmealDTO.getName())));//用套餐名查出id 然后删除再添加实现修改功能

        addSetmeal(setmealDTO);
    }
//根据id查询套餐
    @Override
    public SetmealDTO selectById(Long id) {
        SetmealDTO setmealDTO = new SetmealDTO();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealDTO);
        List<SetmealDish> setmealDish = setmealDishMapper.selectById(id);
        BeanUtils.copyProperties(setmealDish,setmealDTO);

        return setmealDTO;
    }
//套餐启停
    @Override
    public void sos(String id, String status) {
        setmealMapper.sos(Integer.valueOf(id),status);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
